package com.linxz.framework.utils;

import android.graphics.Bitmap;


import com.linxz.framework.configuration.ConfigKeys;
import com.linxz.framework.configuration.Linxz;
import com.linxz.utils.FileUtil;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXImageObject;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXTextObject;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

/**
 * @author Linxz
 * 创建日期：2019年10月19日 14:09
 * version：v4.5.4
 * 描述：
 */
public class ShareSDKManager {

    public static void wxShareText(String content){
        IWXAPI api= WXAPIFactory.createWXAPI(Linxz.getApplicationContext(), Linxz.getConfiguration(ConfigKeys.WE_CHAT_APP_ID).toString(), true);
        if (!api.isWXAppInstalled()){
            ToastUtils.showToast("微信客户端未安装");
            return;
        }
        //初始化一个 WXTextObject 对象，填写分享的文本内容
        WXTextObject textObj = new WXTextObject();
        textObj.text = content;
        //用 WXTextObject 对象初始化一个 WXMediaMessage 对象
        WXMediaMessage msg = new WXMediaMessage();
        msg.mediaObject = textObj;
        msg.description = content;
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = buildTransaction("text");
        req.message = msg;
        req.scene = SendMessageToWX.Req.WXSceneSession;
        //调用api接口，发送数据到微信
        api.sendReq(req);

    }


    public static void wxShare(String url){
        IWXAPI api= WXAPIFactory.createWXAPI(Linxz.getApplicationContext(), Linxz.getConfiguration(ConfigKeys.WE_CHAT_APP_ID).toString(), true);
        if (!api.isWXAppInstalled()){
            ToastUtils.showToast("微信客户端未安装");
            return;
        }
        WXWebpageObject webpage = new WXWebpageObject();
        webpage.webpageUrl =url;
       //用 WXWebpageObject 对象初始化一个 WXMediaMessage 对象
        WXMediaMessage msg = new WXMediaMessage(webpage);
        msg.title ="我正在使用\"虾玩APP\"给你分享一个有趣的人";
        msg.description ="加入我们吧，一起学习、一起浪，一个B格的平台等你到来...";
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.message =msg;
        req.scene = SendMessageToWX.Req.WXSceneSession;
        api.sendReq(req);
    }

    public static void wxFriendsShare(String url){
        IWXAPI api= WXAPIFactory.createWXAPI(Linxz.getApplicationContext(), Linxz.getConfiguration(ConfigKeys.WE_CHAT_APP_ID).toString(), true);
        if (!api.isWXAppInstalled()){
            ToastUtils.showToast("微信客户端未安装");
            return;
        }
        WXWebpageObject webpage = new WXWebpageObject();
        webpage.webpageUrl =url;
        //用 WXWebpageObject 对象初始化一个 WXMediaMessage 对象
        WXMediaMessage msg = new WXMediaMessage(webpage);
        msg.title ="我正在使用\"虾玩APP\"给你分享一个有趣的人";
        msg.description ="加入我们吧，一起学习、一起浪，一个B格的平台等你到来...";
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.message =msg;
        req.scene = SendMessageToWX.Req.WXSceneSession;
        api.sendReq(req);
    }


    public static void wxFriendsShare(String url,String tittle,String content){
        IWXAPI api= WXAPIFactory.createWXAPI(Linxz.getApplicationContext(), Linxz.getConfiguration(ConfigKeys.WE_CHAT_APP_ID).toString(), true);
        if (!api.isWXAppInstalled()){
            ToastUtils.showToast("微信客户端未安装");
            return;
        }
        WXWebpageObject webpage = new WXWebpageObject();
        webpage.webpageUrl =url;
        //用 WXWebpageObject 对象初始化一个 WXMediaMessage 对象
        WXMediaMessage msg = new WXMediaMessage(webpage);
        msg.title =tittle;
        msg.description =content;
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.message =msg;
        req.scene = SendMessageToWX.Req.WXSceneSession;
        api.sendReq(req);
    }


    public static void wxZoneShare(String url){
        IWXAPI api= WXAPIFactory.createWXAPI(Linxz.getApplicationContext(), Linxz.getConfiguration(ConfigKeys.WE_CHAT_APP_ID).toString(), true);
        if (!api.isWXAppInstalled()){
            ToastUtils.showToast("微信客户端未安装");
            return;
        }
        WXWebpageObject webpage = new WXWebpageObject();
        webpage.webpageUrl =url;
        //用 WXWebpageObject 对象初始化一个 WXMediaMessage 对象
        WXMediaMessage msg = new WXMediaMessage(webpage);
        msg.title ="我正在使用\"虾玩APP\"给你分享一个有趣的人";
        msg.description ="加入我们吧，一起学习、一起浪，一个B格的平台等你到来...";
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.message =msg;
        req.scene = SendMessageToWX.Req.WXSceneTimeline;
        api.sendReq(req);
    }


    public static void wxZoneShare(String url,String tittle,String content){
        IWXAPI api= WXAPIFactory.createWXAPI(Linxz.getApplicationContext(), Linxz.getConfiguration(ConfigKeys.WE_CHAT_APP_ID).toString(), true);
        if (!api.isWXAppInstalled()){
            ToastUtils.showToast("微信客户端未安装");
            return;
        }
        WXWebpageObject webpage = new WXWebpageObject();
        webpage.webpageUrl =url;
        //用 WXWebpageObject 对象初始化一个 WXMediaMessage 对象
        WXMediaMessage msg = new WXMediaMessage(webpage);
        msg.title =tittle;
        msg.description =content;
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.message =msg;
        req.scene = SendMessageToWX.Req.WXSceneTimeline;
        api.sendReq(req);
    }


    public static void wxShareBitmap(Bitmap bmp){
        IWXAPI api= WXAPIFactory.createWXAPI(Linxz.getApplicationContext(), Linxz.getConfiguration(ConfigKeys.WE_CHAT_APP_ID).toString(), true);
        if (!api.isWXAppInstalled()){
            ToastUtils.showToast("微信客户端未安装");
            return;
        }
        WXImageObject imgObj = new WXImageObject(bmp);
        WXMediaMessage msg = new WXMediaMessage();
        msg.mediaObject = imgObj;
        Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, 100, 100, true);
        bmp.recycle();
        msg.thumbData = FileUtil.bmpToByteArray(thumbBmp, true);
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = buildTransaction("img");
        req.message = msg;
        req.scene = SendMessageToWX.Req.WXSceneSession;
        api.sendReq(req);
    }



    public static void qqShare(String url){

    }

    public static void qqZoneShare(String url){}


    public static void appShare(String userId){}


    private static String buildTransaction(final String type) {
        return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
    }
}
